import 'package:flutter/foundation.dart';
import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';
import 'package:loxen/mode/material/material_info.dart';
import 'package:loxen/mode/material/material_info_bean.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:rxdart/rxdart.dart';
import 'package:loxen/api/api.dart';
import 'package:loxen/viewmode/base_view_model.dart';

class MaterialDetailViewModel extends BaseViewModel {
  MaterialInfoBean _infoBean;

  MaterialInfoBean get infoBean => _infoBean;

  void changeEntity() {
    notifyListeners();
  }

  void thisCollection() {
    _infoBean.data.results.is_collect = 1;
    _infoBean.data.results.collect_num += 1;
    notifyListeners();
  }

  //素材取消收藏
  void unThisCollection({Function(String msg) onShow}) {
    ApiService.unCollection(
            "material-product", _infoBean.data.results.id.toString())
        .listen((event) {
      if (event.statusCode == 200) {
        _infoBean.data.results.is_collect = 0;
        _infoBean.data.results.collect_num -= 1;
        if (onShow != null) onShow.call(StringResources.qu_xiao_shou_cang);
        notifyListeners();
      } else {
        if (onShow != null) onShow.call(StringResources.qu_xiao_shou_cang_shi_bai);
      }
    }, onError: (error, stack) {
      if (onShow != null) onShow.call(StringResources.wang_luo_yi_chang);
      print(error);
      print(stack);
    });
  }

  //取消收藏
  void unCollection(InspirationInfo likeInspiration) {
    ApiService.unCollection("idea-article", likeInspiration.id.toString())
        .listen((event) {
      if (event.statusCode == 200) {
        likeInspiration.is_collect = 0;
        likeInspiration.collect_num -= 1;
        notifyListeners();
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
    });
  }

  //猜你喜欢文章点赞
  void likeInspiration(InspirationInfo likeInspiration) {
    ApiService.likeArticle(likeInspiration.id.toString()).listen((event) {
      likeInspiration.is_like = 1;
      likeInspiration.like_num += 1;
      notifyListeners();
    }, onError: (error) => print(error));
  }

  //猜你喜欢文章取消点赞
  void unLikeInspiration(InspirationInfo likeInspiration) {
    ApiService.unLikeArticle(likeInspiration.id.toString()).listen((event) {
      likeInspiration.is_like = 0;
      likeInspiration.like_num -= 1;
      notifyListeners();
    }, onError: (error) => print(error));
  }

  //点赞
  void likeThis() {
    ApiService.likeMaterial(_infoBean.data.results.id).listen((event) {
      if (event.statusCode == 201) {
        _infoBean.data.results.is_like = 1;
        _infoBean.data.results.like_num += 1;
        notifyListeners();
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
    });
  }

  //取消点赞
  void unlikeThis() {
    ApiService.unlikeMaterial(_infoBean.data.results.id).listen((event) {
      if (event.statusCode == 200) {
        _infoBean.data.results.is_like = 0;
        _infoBean.data.results.like_num -= 1;
        notifyListeners();
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
    });
  }

  void loadData(int id) {
    if (mState == BaseState.LOADING) return;
    ApiService.materialInfo(id)
        .doOnListen(() => mState = BaseState.LOADING)
        .flatMap(
            (value) => compute(decodeMaterialInfoBean, value.data).asStream())
        .listen((event) {
      _infoBean = event;
      mState = BaseState.CONTENT;
    }, onError: (error, stack) {
      print(error);
      print(stack);
      mState = BaseState.FAIL;
    }, onDone: () => notifyListeners());
  }

  static MaterialInfoBean decodeMaterialInfoBean(dynamic json) {
    return MaterialInfoBean.fromJson(json);
  }
}
